/*
 * Copyright (c) 2011 Jean-Marc Perronne
 * Ensisa, 12 rue des Freres Lumieres, F-68093 Mulhouse cedex
 * All rights reserved.
 *
 * The copyright of this software is owned by Werner Randelshofer. 
 * You may not use, copy or modify this software, except in  
 * accordance with the license agreement you entered into with  
 * Jean-Marc Perronne. For details see accompanying license terms. 
 */

package fr.uha.ensisa.jmp.drone.ui.instrument;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Hashtable;
import java.util.Map.Entry;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSlider;

import fr.uha.ensisa.jmp.drone.ui.instrument.linear.LinearIndicator;
import fr.uha.ensisa.jmp.ui.utils.JMSwingUtilities;
import fr.uha.ensisa.jmp.ui.utils.PassiveSlider;


public class TestInstruments
{

	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Sample Sliders");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//battery
		
		final PassiveSlider batteryJauge = new PassiveSlider(JSlider.HORIZONTAL, 0, 100, 50);
		batteryJauge.setMajorTickSpacing(25);
		batteryJauge.setMinorTickSpacing(5);
		batteryJauge.setPaintTicks(true);
		batteryJauge.setPaintLabels(true);
		batteryJauge.setPaintTrack(true);
		batteryJauge.setPaintTicks(true);

		Hashtable<Integer, JLabel> labels =  (Hashtable<Integer, JLabel>) batteryJauge.getLabelTable();
		for(Entry<Integer, JLabel> e :labels.entrySet())
		{
			if(e.getKey().intValue() < 25) e.getValue().setForeground(Color.RED);
			else if(e.getKey().intValue() < 40) e.getValue().setForeground(Color.ORANGE);
			else if(e.getKey().intValue() < 50) e.getValue().setForeground(Color.YELLOW);
			else  e.getValue().setForeground(Color.GREEN);
		}
		
		
		JSlider js1 = new JSlider(JSlider.VERTICAL);
		js1.setMaximum(180);
		js1.setMinimum(-180);
		js1.setValue(0);
		
		JSlider js2 = new JSlider(JSlider.VERTICAL);
		js2.setMaximum(90);
		js2.setMinimum(-90);
		js2.setValue(0);

		System.out.println(js1.getUI());

		final LinearIndicator b1 = new LinearIndicator("battery", SwingConstants.VERTICAL);
		//b1.setValue(50);
		
		final Compass compass = new Compass("pictures/compass_background.png", "pictures/compass_needle.png" );
		compass.getModel().setMinimum(-180);
		compass.getModel().setMaximum(180);

		
		final Horizon horizon = new Horizon( "pictures/horizon_plane.png" );
		horizon.getRenderer().setSize(new Dimension(200,200));
		
		js1.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent changeEvent)
			{
				Object source = changeEvent.getSource();
				JSlider s = (JSlider) source;
				if (!s.getValueIsAdjusting());
				{

					System.out.println(s.getValue());
					//batteryJauge.setValue(s.getValue());
					//compass.getModel().setValue(s.getValue());
					horizon.getModel().setValue("roll",s.getValue());
				}
			}
		});
		
		js2.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent changeEvent)
			{
				Object source = changeEvent.getSource();
				JSlider s = (JSlider) source;
				if (!s.getValueIsAdjusting());
				{
					horizon.getModel().setValue("pitch",s.getValue());
				}
			}
		});

		//frame.add(batteryJauge, BorderLayout.EAST);
		frame.add(js1, BorderLayout.WEST);
		frame.add(js2, BorderLayout.EAST);

		frame.add(b1, BorderLayout.CENTER);
		//frame.add(compass,BorderLayout.PAGE_END);
		frame.add(horizon,BorderLayout.PAGE_START);

		frame.setBackground(Color.DARK_GRAY);
		
		b1.labelColor(JMSwingUtilities.invertColor(Color.DARK_GRAY));

		frame.setSize(300, 200);
		frame.setVisible(true);

	}

}
